function init() {
  initShowList();
}

// 获取当前 list 数据
function getReadList() {
  const getListDOM = document.querySelector('.list-tbody');
  let readList = localStorage.getItem('list') || '[]';
  readList = JSON.parse(readList);
  getListDOM.innerHTML = '';
  console.log(readList)
  if(readList != false) {
    return readList;
  } else {
    return false;
  }
}

function initShowList() {
  const listData = getReadList();
  const getListDOM = document.querySelector('.list-tbody');
  if (listData) {
    document.querySelector('.form-page').innerHTML = listData.length;
    for (let i = 0; i < listData.length; i++) {
      var tr = document.createElement("tr");
      tr.innerHTML = `
          <th scope="row">${i + 1}</th>
          <td>
            <a href="courseinfo.html?id=${i}" title="点击查看详细信息">${listData[i].title}</a>
          </td>
          <td>${listData[i].codeNum}</td>
          <td>${listData[i].state}</td>
          <td>${listData[i].rule}</td>
          <td>${listData[i].major}</td>
          <td>${listData[i].way}</td>
          <td>
            <button type="button" class="btn btn-edit icon iconfont"  onclick="window.location.href='coursedit.html?id=${i}'" title="编辑">
              &#xe66e;
            </button>
            <button type="button" class="btn btn-del del icon iconfont" data-del data-value="${i}" title="删除" onclick="handleClickDel(${i})">
              &#xe666;
            </button>
          </td>
        `
      getListDOM.appendChild(tr);
    }
  } else {
    document.querySelector('.form-page').innerHTML = 0;
    document.querySelector(".nullShow").style.display = 'block';
  }
}

function handleClickDel(i) {
  // 调用模态框
  modal({
    title: '提示',
    content: '如果要删除该消息，请选择确认按钮；如果误触，请单机右上角取消按钮或取消按钮；',
    width: '500px',
    height: 'auto',
    cancelBtn: true,
    confirmBtn: true,
    cancelText: '取消',
    confirmText: '确认',
    cancelCallback: function() {},
    confirmCallback: function() {
      const listData = JSON.parse(localStorage.getItem('list'));
      listData.splice(i, 1);
      let listChangeString = JSON.stringify(listData);
      localStorage.setItem('list', listChangeString);
      initShowList();
    }
  });
}

init();